<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="params.orderNo" placeholder="订单号" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="fetchData" />
      <el-input v-model="params.receiverName" placeholder="收货人名称" style="width: 200px;margin-left: 10px;" class="filter-item" clearable @keyup.enter.native="fetchData" />
      <el-input v-model="params.goodsName" placeholder="货品名称" style="width: 200px;margin-left: 10px;" class="filter-item" clearable @keyup.enter.native="fetchData" />
      <el-select v-model="params.payStatus" class="filter-item" style="width:200px;margin-left: 10px;" placeholder="请选择付款状态">
          <el-option key="1" label="已付款" :value="1" />
          <el-option key="0" label="未付款" :value="0" />
      </el-select>
      <el-select v-model="params.goodsStatus" class="filter-item" style="width:200px;margin-left:10px;margin-right:10px" placeholder="请选择货品状态">
          <el-option key="0" label="未发货" :value="0" />
          <el-option key="1" label="已到货" :value="1" />
          <el-option key="2" label="已发货" :value="2" />
          <el-option key="3" label="已收货" :value="3" />
      </el-select>
        <el-date-picker
          v-model="Time"
          type="datetimerange"
          range-separator="至"
          format="yyyy-MM-dd HH:mm:ss"
          @change="timeChange"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="fetchData">
        搜索
      </el-button>
      <el-button type="success" icon="el-icon-warning-outline" class="filter-item" @click="resetForm()">重置</el-button>

    </div>
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="editOrAdd(0,0)">
        添加
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"

      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="订单号">
        <template slot-scope="scope">
          {{ scope.row.orderNo }}
        </template>
      </el-table-column>
      <el-table-column label="收货人" align="center">
        <template slot-scope="scope">
          {{ scope.row.receiverName }}
        </template>
      </el-table-column>
      <el-table-column label="收货人电话" align="center">
        <template slot-scope="scope">
          {{ scope.row.receiverPhone }}
        </template>
      </el-table-column>
            <el-table-column label="货品名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.goodsName }}
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.payStatus==0">未付款</div>
          <div v-if="scope.row.payStatus==1">已付款</div>
        </template>
      </el-table-column>
      <el-table-column label="货品状态" align="center">
          <template slot-scope="scope">
          <div v-if="scope.row.goodsStatus==0">未发货</div>
          <div v-if="scope.row.goodsStatus==1">已到货</div>
          <div v-if="scope.row.goodsStatus==2">已发货</div>
          <div v-if="scope.row.goodsStatus==3">已收货</div>
        </template>
      </el-table-column>
        <el-table-column label="开始时间" align="center">
        <template slot-scope="scope">
            {{scope.row.createTime}}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center">
        <template slot-scope="scope">
            {{scope.row.endTime?'scope.row.endTime':'暂无'}}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button type="text" @click="editOrAdd(1,scope.row)">编辑</el-button>
            <el-button  type="text" style="color:red" @click="deleteUser(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="params.pageNum" :limit.sync="params.pageSize" @pagination="fetchData" />

    <el-dialog :title="dialogTitles[dialogIndex]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="adminData" label-width="140px" style="margin:0 140px 0 80px">
        
        <el-form-item v-if="dialogIndex==1" label="订单号" prop="orderNo">
          <el-input v-model="adminData.orderNo"  />
        </el-form-item>
        <el-form-item label="货品图片" prop="goodsImage">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="adminData.goodsImage" :src="adminData.goodsImage" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="货品名称" prop="goodsName">
          <el-input v-model="adminData.goodsName"  />
        </el-form-item>
        <el-form-item label="收货人名称" prop="receiverName">
          <el-input v-model="adminData.receiverName" />
        </el-form-item>
        <el-form-item label="收货人手机号" prop="receiverPhone">
          <el-input v-model="adminData.receiverPhone" />
        </el-form-item>
        <el-form-item label="规格" prop="specifications">
          <el-input v-model="adminData.specifications" />
        </el-form-item>
         <el-form-item label="价格" prop="price">
          <el-input v-model="adminData.price"  type="number"/>
        </el-form-item>
           <el-form-item label="币种" prop="currencyName">
            <el-select v-model="adminData.currencyName" @change="currencyNameChange" class="filter-item" style="width:100%" placeholder="请选择币种">
              <el-option v-for="item in bzList"  :key="item.currencyName" :label="item.currencyName" :value="item.currencyName" />
          </el-select>
        </el-form-item>
        <el-form-item label="汇率" prop="exchangeRate">
          <el-input disabled v-model="adminData.exchangeRate" />
        </el-form-item>
        <el-form-item label="运费" prop="freight">
          <el-input type="number" v-model="adminData.freight" />
        </el-form-item>
         <el-form-item label="总付款" prop="totalPayment">
          <el-input type="number" v-model="adminData.totalPayment" />
        </el-form-item>
        <el-form-item label="付款状态" prop="payStatus">
        <el-select v-model="adminData.payStatus" class="filter-item" style="width:100%" placeholder="请选择付款状态">
            <el-option key="1" label="已付款" :value="1" />
            <el-option key="0" label="未付款" :value="0" />
         </el-select>
        </el-form-item>
        <el-form-item label="货品状态" prop="goodsStatus">
        <el-select v-model="adminData.goodsStatus" class="filter-item" style="width:100%" placeholder="请选择货品状态">
                <el-option key="0" label="未发货" :value="0" />
                <el-option key="1" label="已到货" :value="1" />
                <el-option key="2" label="已发货" :value="2" />
                <el-option key="3" label="已收货" :value="3" />
         </el-select>
        </el-form-item>
        <el-form-item label="快递公司" prop="expressCompany">
          <el-input  v-model="adminData.expressCompany" />
        </el-form-item>
        <el-form-item label="快递单号" prop="oddNum">
          <el-input  v-model="adminData.oddNum" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input    type="textarea" :rows="4" placeholder="请输入内容" v-model="adminData.remarks" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">
          取消
        </el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { orderList,deleteOrder,addOrUpdate,nopage} from '@/api/user'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import Calendar from '@/components/Cascader'
import { nowPassword } from '../../utils/constant'
export default {
  components: { Pagination, Calendar },
  directives: { waves },
  data() {
    return {
      Time:'',
      action:process.env.VUE_APP_BASE_API+"/sysUser/upImgs.json",
      list: [],
      bzList:[],
      listLoading: true,
      total: 10,
      dialogTitles: ['添加', '编辑'],
      dialogIndex: 0,
      dialogFormVisible: false,
      adminData: {},
      params: {
        pageNum: 1,
        pageSize: 10,
        orderNo: '',
        receiverName: '',
        receiverName: '',
        payStatus: '',
        goodsStatus: '',
        startTime: '',
        endTime: '',
      },
      rules: {
          receiverName: [{ required: true, message: '请输入收货人', trigger: 'blur' }],
          receiverPhone: [{ required: true, message: '请输入收货人手机号', trigger: 'blur' }],
          goodsName: [{ required: true, message: '请输入货物名称', trigger: 'blur' }],
           goodsImage: [{ required: true, message: '请上传货品图片', trigger: 'blur' }],
          specifications: [{ required: true, message: '请输入规格', trigger: 'blur' }],
          price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
           currencyName: [{ required: true, message: '请选择币种', trigger: 'blur' }],
          freight: [{ required: true, message: '请输入运费', trigger: 'blur' }],
          totalPayment: [{ required: true, message: '请输入总付款', trigger: 'blur' }],
           payStatus: [{ required: true, message: '请选择付款状态', trigger: 'blur' }],
          goodsStatus: [{ required: true, message: '请选择货品状态', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.fetchData()
    this.getBzList()
  },
  methods: {
    timeChange(value){
      this.params.startTime=this.formatDate(value[0])
      this.params.endTime=this.formatDate(value[1])
    },
      formatTen(num) { 
            return num > 9 ? (num + "") : ("0" + num); 
        },
      formatDate(date) { 
            var date = new Date(date)
            var year = date.getFullYear(); 
            var month = date.getMonth() + 1; 
            var day = date.getDate(); 
            var hour = date.getHours(); 
            var minute = date.getMinutes(); 
            var second = date.getSeconds(); 
            return year + "-" + this.formatTen(month) + "-" + this.formatTen(day)+ " " +this.formatTen(hour)+ ":" +this.formatTen(minute)+ ":" +this.formatTen(second); 
      },
       // 图片上传
      handleAvatarSuccess(res, file) {
        this.adminData.goodsImage='http://39.105.201.40:8081'+res.src;
      },
    fetchData() {
      this.listLoading = true
      orderList(this.params).then(response => {
        this.total = response.total
        this.list = response.rows
        this.listLoading = false
      })
    },
    getBzList() {
      nopage(this.params).then(response => {
        this.bzList = response
      })
    },
    cancel() {
      this.dialogFormVisible = false
    },
    currencyNameChange(value){
      this.bzList.forEach((i)=>{
        if(value==i.currencyName){
          this.adminData.exchangeRate=i.exchangeRate
        }
      })
       this.adminData.totalPayment=Number(this.adminData.price)*Number(this.adminData.exchangeRate)+Number(this.adminData.freight)
    },
    editOrAdd(index, item) {
      this.adminData = Object.assign({}, item)
      if (index === 1) {
        delete this.adminData.createTime
        delete this.adminData.updateTime
        
      }
      this.dialogIndex = index
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    save() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addOrUpdate(this.adminData).then(res => {
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: this.dialogTitles[this.dialogIndex] + '成功!'
            })
            this.fetchData()
          })
        }
      })
    },
    updateStatus(id) {
      updateStatus(id).then(res => {
        this.$message({
          type: 'success',
          message: '成功!'
        })
        this.fetchData()
      })
    },
    deleteUser(id) {
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOrder({id:id}).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      })
    },
    resetForm() {
      this.params = {
        pageNum: 1,
        pageSize: 10
      }
      this.$nextTick(() => {
        this.fetchData()
      })
    }
  },
  watch: {
    'adminData.price': {
      handler: function() {
        // if (!this.adminData.price) {
        //   this.adminData.price = null
        //   return
        // }
         this.adminData.totalPayment=Number(this.adminData.price)*Number(this.adminData.exchangeRate)+Number(this.adminData.freight)
      }
    },
    'adminData.freight': {
      handler: function() {
        // if (!this.adminData.freight) {
        //   this.adminData.freight = null
        //   return
        // }
         this.adminData.totalPayment=Number(this.adminData.price)*Number(this.adminData.exchangeRate)+Number(this.adminData.freight)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
    /deep/.el-dialog{
            width:40%
    }
    /deep/.el-date-editor{
      vertical-align: top;
    }
        /deep/.el-upload{
      border: 1px solid #d9d9d9;
      border-radius: 5px;
      margin: 0 auto;
    }
    /deep/.el-form{
      height: 600px;

      overflow-y:scroll;
    }
    /deep/.el-dialog__header{
      border-bottom: 1px solid #d9d9d9;
    }
     /deep/.el-dialog__footer{
      border-top: 1px solid #d9d9d9;
    }
    .el-form::-webkit-scrollbar{
      display:none;
    }
     .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
